<template>
  <div class="login-bg">
    <el-card class="login-card">
      <template v-if="mode === 'login'">
        <h2 class="login-title">智能问答助手登录</h2>
        <el-form :model="loginForm" @submit.prevent="onLogin" label-width="80px" label-position="left" class="login-form">
          <el-form-item label="用户名">
            <el-input v-model="loginForm.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="loginForm.password" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item label=" " label-width="80px" class="btn-item">
            <el-button type="primary" @click="onLogin" :loading="loading" class="login-btn" auto-insert-space>登录</el-button>
          </el-form-item>
        </el-form>
        <div class="register-tip">
          <span>还没有账号？</span>
          <el-button type="text" @click="mode = 'register'">点击注册</el-button>
        </div>
      </template>
      <template v-else>
        <h2 class="login-title">注册新账号</h2>
        <el-form :model="registerForm" @submit.prevent="onRegister" label-width="80px" label-position="left" class="login-form">
          <el-form-item label="用户名">
            <el-input v-model="registerForm.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="registerForm.password" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item label=" " label-width="80px" class="btn-item">
            <el-button type="primary" @click="onRegister" :loading="loading" class="login-btn" auto-insert-space>注册</el-button>
          </el-form-item>
        </el-form>
        <div class="register-tip">
          <span>已有账号？</span>
          <el-button type="text" @click="mode = 'login'">返回登录</el-button>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const loading = ref(false);
const mode = ref('login');
const loginForm = ref({ username: '', password: '' });
const registerForm = ref({ username: '', password: '' });

const onLogin = async () => {
  loading.value = true;
  try {
    const res = await axios.post('/api/auth/login', loginForm.value);
    if (res.data.success) {
      localStorage.setItem('userId', res.data.userId);
      ElMessage.success('登录成功');
      router.push('/chat');
    } else {
      ElMessage.error(res.data.msg);
    }
  } catch (e) {
    ElMessage.error('登录失败');
  }
  loading.value = false;
};

const onRegister = async () => {
  loading.value = true;
  try {
    const res = await axios.post('/api/auth/register', registerForm.value);
    if (res.data.success) {
      ElMessage.success('注册成功，请登录');
      mode.value = 'login';
    } else {
      ElMessage.error(res.data.msg);
    }
  } catch (e) {
    ElMessage.error('注册失败');
  }
  loading.value = false;
};
</script>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f2f5;
  z-index: 0;
}
.login-card {
  width: 520px;
  max-width: 90vw;
  box-shadow: 0 4px 24px #e0e2e8;
  padding: 48px 32px 32px 32px;
  z-index: 1;
}
.login-title {
  text-align: center;
  margin-bottom: 32px;
  font-size: 2.2rem;
  font-weight: bold;
}
.el-form-item__label {
  justify-content: flex-start !important;
  text-align: left !important;
}
.login-form .el-form-item {
  margin-bottom: 28px;
}
.btn-item .el-form-item__content {
  margin-left: 0 !important;
  padding-left: 0 !important;
}
.login-btn {
  width: 100%;
  margin-left: 0;
  display: block;
}
.register-tip {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 8px;
  font-size: 15px;
}
</style> 